//
// Created by PC on 2017/11/10.
//

#ifndef SELECTIONSORT_SORTTESTHELPER_H
#define SELECTIONSORT_SORTTESTHELPER_H

#include <iostream>
#include <ctime>
#include <assert.h>

using namespace std;

namespace SortTestHelper{
    int* generateRandomArray(int n,int rangeL,int rangeR){
        assert(rangeL<=rangeR);
        srand(time(NULL));
        int* arr=new int[n];
        for(int i=0;i<n;i++)
            arr[i]=rand()%(rangeR-rangeL+1)+rangeL;
        return arr;
    }
    template <typename T>
    void printfArray(T arr[],int n){
        for (int i = 0; i < n; i++)
            cout<<arr[i]<<" ";
        return;
    }

    template <typename T>
    bool isSorted(T arr[],int n){
        for(int i=0;i<n-1;i++)
            if(arr[i]>arr[i+1])
                return false;
        return  true;
    }

    template <typename T>
    void testSort(string s,void(*sort)(T[],int ),T arr[],int n){

        clock_t clock1=clock();
        sort(arr,n);
        clock_t clock2=clock();

        assert(isSorted(arr,n));
        cout<<s<<" : "<<double(clock2-clock1)/CLOCKS_PER_SEC<<" s"<<endl;
    }

    int *copyArray(int a[], int n) {
        int *arr = new int[n];
        copy(a, a + n, arr);
        return arr;
    }

    int *generateNearlyOrderedArray(int n, int swapTimes) {
        int *arr=new int[n];
        for (int i = 0; i < n; ++i)
            arr[i]=i;
        srand(time(NULL));
        for (int j = 0; j < swapTimes; ++j) {
            int pox=rand()%n;
            int poy=rand()%n;
            swap(arr[pox],arr[poy]);
        }
        return arr;
    }

}
#endif //SELECTIONSORT_SORTTESTHELPER_H
